Ext.define('Admin.view.page.operation.detection.DetectionController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.operation-detection',

    /**
     * 自定义宽高
     * 加载页面的基础数据到model中存储
     * @param {*} me 
     * @param {*} eOpts 
     */
     yeAfterrender(me, eOpts ){
        /*         
        //自定义宽高 -无需设置
        references.menuGridPanel.setWidth( 2/3 * allWidth )
        references.projectGridPanel.setWidth( 1/3 * allWidth )
        references.menuGridPanel.setHeight(allHeight)
        references.projectGridPanel.setHeight(allHeight)
         */
        // 将页面主要组件加入到data中缓存
        this.getViewModel().set('topComponent', me.ownerCt);
        this.getViewModel().set('pageComponent', me);
        // debugger
        //页面初始化
        let model = this.getViewModel()
        let references = model.get('pageComponent').getReferences()
        let menuGridPanel = references.menuGridPanel
        //如果存储再store中就要用获取stroe的model 来刷新界面;初始化时
        model.get('projectStore').reload({
            params: {
                groupType : 2,
                keyword : '',
            },
        })
    },
    yeMenuGridAfterrender(me,opt){
    },
    yeProjectGridAfterrender(me,opt){
    },
    /**
     * 按钮点击
     */
    addMenuButton(me, event) {
        let model = this.getViewModel()
        // debugger
        let references = model.get('pageComponent').getReferences()
        let menuGridPanel = references.menuGridPanel
        let last = menuGridPanel.getStore().data.length + 1
        // 插入数据, 将光标锁定到新增的一行;
        let rec = Ext.create('Admin.model.group.menu.Menu',{
            id : null,
            parent_id : null,
            name : '',
            sort : last,
            type : '2', //1-菜单组 ，2 -任务组
        })
        rec.set('id', null);
        menuGridPanel.getStore().insert(last, rec);
        menuGridPanel.getSelectionModel().selectRange(last - 1,last - 1);
        // 光标锁定到新增的数据第一个单元格上
        // let cellediting = menuGridPanel.getPlugins().filter(i => i.ptype == 'cellediting')[0]
        // cellediting.startEditByPosition({
        //     row: last - 1,
        //     column: 0
        // });
    },
    saveMenuButton(me, event) {
        let model = this.getViewModel()
        let references = model.get('pageComponent').getReferences()
        let menuGridPanel = references.menuGridPanel
        let dataStore = menuGridPanel.getStore().getModifiedRecords()
        let data = []
        if(dataStore.length <= 0 ) {
            Ext.Msg.alert("提示", "保存失败,数据未发生更改");
            return
        }
        dataStore.forEach(item => {
            data.push(item.data)
        })
        // debugger
        Ext.Ajax.request({  
            method : 'POST',  
            url : '/ye/group/saveOrUpdate',  
            jsonData : data,
            success : function(response, config) {  
                var json = Ext.decode(response.responseText)
                if (json.success == true) {  
                    menuGridPanel.getStore().reload({
                        params: {
                            groupType : 2,
                            keyword : '',
                        },
                    })
                    Ext.Msg.alert("提示", '保存成功!')
                } else {  
                    Ext.Msg.alert('提示', '保存失败!')
                }  
            },  
            failure : function() {  
                Ext.Msg.alert('提示', '保存失败!')
            }  
        });          
    },
    deleteMenuProjectButton(me, event) {
        let deleteFunc = () => {
            let model = this.getViewModel()
            let references = model.get('pageComponent').getReferences()
            let menuGridPanel = references.menuGridPanel
            // let dataSelected = menuGridPanel.getSelectionModel().getSelected()
            // dataSelected.each(item => {console.log(item)})
            // dataSelected.map
            let dataSelected = menuGridPanel.getSelectionModel().getSelection() 
            let datas = []
            if(dataSelected.length <= 0 ) {
                Ext.Msg.alert('提示', '未选择数据!')
                return
            }
            dataSelected.forEach(item => {
                if(!item.data.id) return 
                datas.push(item.data.id)
            })
            if(datas.length <= 0) {
                Ext.Msg.alert('提示', '数据还未保存无法删除!')
                return
            }
            Ext.Ajax.request({  
                method : 'GET',  
                url : '/ye/group/delete',  
                params : {  
                    ids : datas.join(','),
                    type : 'all',
                },  
                // jsonData : data,
                success : function(response, config) {  
                    var json = Ext.decode(response.responseText)
                    if (json.success == true) {  
                        menuGridPanel.getStore().reload({
                            params: {
                                groupType : 2,
                                keyword : '',
                            },
                        })
                        Ext.Msg.alert("提示", '删除成功!')
                    } else {  
                        Ext.Msg.alert('提示', '删除失败!')
                    }  
                },  
                failure : function() {  
                    Ext.Msg.alert('提示', '删除失败!')
                }  
            });          
        }
        Ext.Msg.show({
		    title: '提示',
		    msg: '确认删除?',
		    width: 300,
		    buttons: Ext.Msg.OKCANCEL,
		    // animateTarget: tagetBtn,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
	    	   if (btn == 'ok') {
                    deleteFunc()
				}
		    }
		})
    },
    menuGridPanelPagingToolbarBeforechange(me, page, eOpts){
        let model = this.getViewModel()
        me.getStore().getProxy().setExtraParams({
            groupType : 2,
            keyword : model.get('menuGridKeyword'),
            // page : page,
            // limit : 
        })
        return true
        // Ext.apply(eOpts, {
        //     groupType : 1,
        //     keyword : '',
        // });
    },
    queryMenuButton(me, event) {
        let model = this.getViewModel()
        let references = model.get('pageComponent').getReferences()
        let menuGridPanel = references.menuGridPanel
        model.get('projectStore').reload({
            params: {
                groupType : 2,
                keyword : model.get('menuGridKeyword'),
                // page : 1,
            },
        })
    },
});

